<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>node.js下用window.onerror捕获并上报Js错误 | 云飞扬 - 个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="使用场景
捕获前端js异常信息，方便后期优化
方便前端交互持续优化
方便问题重现

环境
node.js
框架：koa
日志模块：log4js

前端代码
使用前端的window.onerror 事件进行监听前端代码的异常。
当前端代码发生异常时，通过post并将异常信息上传服务器。

123456789101112window.onerror = function(errorMessage, s">
<meta property="og:type" content="article">
<meta property="og:title" content="node.js下用window.onerror捕获并上报Js错误">
<meta property="og:url" content="http://blog.yxxit.com/2017/02/09/node.js下用window.onerror捕获并上报Js错误/index.html">
<meta property="og:site_name" content="云飞扬 - 个人博客">
<meta property="og:description" content="使用场景
捕获前端js异常信息，方便后期优化
方便前端交互持续优化
方便问题重现

环境
node.js
框架：koa
日志模块：log4js

前端代码
使用前端的window.onerror 事件进行监听前端代码的异常。
当前端代码发生异常时，通过post并将异常信息上传服务器。

123456789101112window.onerror = function(errorMessage, s">
<meta property="og:image" content="http://blog.yxxit.com/view/img/window-err-b.jpg">
<meta property="og:image" content="http://blog.yxxit.com/view/img/window.error.png">
<meta property="og:updated_time" content="2017-02-09T06:04:30.991Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="node.js下用window.onerror捕获并上报Js错误">
<meta name="twitter:description" content="使用场景
捕获前端js异常信息，方便后期优化
方便前端交互持续优化
方便问题重现

环境
node.js
框架：koa
日志模块：log4js

前端代码
使用前端的window.onerror 事件进行监听前端代码的异常。
当前端代码发生异常时，通过post并将异常信息上传服务器。

123456789101112window.onerror = function(errorMessage, s">
<meta name="twitter:image" content="http://blog.yxxit.com/view/img/window-err-b.jpg">
  
    <link rel="alternate" href="/atom.xml" title="云飞扬 - 个人博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/view/favicon.ico">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/view/css/style.css">
  

</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/view/" id="logo">云飞扬 - 个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/view/">Home</a>
        
          <a class="main-nav-link" href="/view/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://blog.yxxit.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-node.js下用window.onerror捕获并上报Js错误" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/view/2017/02/09/node.js下用window.onerror捕获并上报Js错误/" class="article-date">
  <time datetime="2017-02-09T05:22:50.011Z" itemprop="datePublished">2017-02-09</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      node.js下用window.onerror捕获并上报Js错误
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景"></a>使用场景</h2><ol>
<li>捕获前端js异常信息，方便后期优化</li>
<li>方便前端交互持续优化</li>
<li>方便问题重现</li>
</ol>
<h2 id="环境"><a href="#环境" class="headerlink" title="环境"></a>环境</h2><ul>
<li>node.js</li>
<li>框架：koa</li>
<li>日志模块：log4js</li>
</ul>
<h2 id="前端代码"><a href="#前端代码" class="headerlink" title="前端代码"></a>前端代码</h2><ol>
<li>使用前端的window.onerror 事件进行监听前端代码的异常。</li>
<li>当前端代码发生异常时，通过post并将异常信息上传服务器。</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">window</span>.onerror = <span class="function"><span class="keyword">function</span>(<span class="params">errorMessage, scriptURI, lineNumber, columnNumber, errorObj</span>) </span>&#123;</div><div class="line">    <span class="keyword">var</span> params = &#123;</div><div class="line">        <span class="attr">errorMessage</span>: errorMessage,</div><div class="line">        <span class="attr">scriptURI</span>: scriptURI,</div><div class="line">        <span class="attr">lineNumber</span>: lineNumber,</div><div class="line">        <span class="attr">columnNumber</span>: columnNumber,</div><div class="line">        <span class="attr">errorObj</span>: errorObj</div><div class="line">    &#125;;</div><div class="line">    $.post(<span class="string">'/yourserver/error'</span>, params, <span class="function"><span class="keyword">function</span>(<span class="params">data</span>) </span>&#123;</div><div class="line">        <span class="built_in">console</span>.log(<span class="built_in">JSON</span>.stringify(data));</div><div class="line">    &#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="后端代码"><a href="#后端代码" class="headerlink" title="后端代码"></a>后端代码</h2><ol>
<li>记录前端异常信息</li>
<li>记录发生异常时设备、浏览器信息</li>
<li>记录发生异常的ip地址</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> body = <span class="keyword">this</span>.request.body;</div><div class="line"><span class="keyword">var</span> userAgent = <span class="keyword">this</span>.headers[<span class="string">'user-agent'</span>];</div><div class="line"><span class="keyword">var</span> cookie = <span class="keyword">this</span>.headers[<span class="string">'cookie'</span>];</div><div class="line">logger.error(<span class="string">'--------------------前端异常信息----------------------'</span>);</div><div class="line">logger.error(<span class="string">"手机及浏览器环境："</span> + userAgent);</div><div class="line">logger.error(<span class="string">"ip地址："</span> + <span class="keyword">this</span>.ip);</div><div class="line">logger.error(<span class="string">"cookie："</span> + cookie);</div><div class="line">logger.error(<span class="string">"错误信息："</span> + body.errorMessage);</div><div class="line">logger.error(<span class="string">"出错文件："</span> + body.scriptURI);</div><div class="line">logger.error(<span class="string">"出错行号："</span> + body.lineNumber);</div><div class="line">logger.error(<span class="string">"出错列号："</span> + body.columnNumber);</div><div class="line">logger.error(<span class="string">"错误详情："</span> + body.errorObj);</div><div class="line">logger.error(<span class="string">"----------------------end-----------------------------"</span>);</div><div class="line"><span class="keyword">this</span>.body = &#123;<span class="attr">status</span>:<span class="number">200</span>,<span class="attr">data</span>:<span class="string">'ok'</span>&#125;;</div></pre></td></tr></table></figure>
<h2 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h2><p>浏览器端：<br><img src="/view/img/window-err-b.jpg" alt="浏览器端异常信息"></p>
<p>服务器端：<br><img src="/view/img/window.error.png" alt="服务器异常信息"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://blog.yxxit.com/view/2017/02/09/node.js下用window.onerror捕获并上报Js错误/" data-id="ciyxz14uy0000kwddpr4jmltf" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/view/2017/02/10/tool/常用工具/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          开发常用工具
        
      </div>
    </a>
  
  
    <a href="/view/2017/01/22/Spring.boot 项目创建之傻瓜教程/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Spring.boot 项目创建之傻瓜教程</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/view/tags/node-js-规范/">node.js, 规范</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/view/tags/node-js-规范/" style="font-size: 10px;">node.js, 规范</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/view/archives/2017/03/">March 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/view/archives/2017/02/">February 2017</a></li><li class="archive-list-item"><a class="archive-list-link" href="/view/archives/2017/01/">January 2017</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/view/2017/03/21/npm/">npm 常用命令</a>
          </li>
        
          <li>
            <a href="/view/2017/03/21/git/">Git 常用命令</a>
          </li>
        
          <li>
            <a href="/view/2017/03/05/windows nginx natapp/">windows nginx natapp</a>
          </li>
        
          <li>
            <a href="/view/2017/02/17/node.js 模块之 pm2/">node.js 模块之 pm2</a>
          </li>
        
          <li>
            <a href="/view/2017/02/16/node.js 6.9.4 升级/">node.js 6.9.4 升级</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 coolshaw<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/view/" class="mobile-nav-link">Home</a>
  
    <a href="/view/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>


  <link rel="stylesheet" href="/view/fancybox/jquery.fancybox.css">
  <script src="/view/fancybox/jquery.fancybox.pack.js"></script>


<script src="/view/js/script.js"></script>

  </div>
</body>
</html>